package com.tubemostwanted.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubemostwanted.entity.Tube;
import com.tubemostwanted.entity.Tag;
import com.tubemostwanted.model.TagModel;
import com.tubemostwanted.model.TubeModel;

public class KeywordController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static int itemPerPage = 12;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		// search by input key.
		try {
			String path = ((HttpServletRequest) request).getRequestURI();
			String[] splittedURI = path.split("/");
			int page = 1;
			String keyword = "";
			if (splittedURI.length == 3) {
				keyword = splittedURI[splittedURI.length - 1];
			} else if (splittedURI.length == 4) {
				keyword = splittedURI[splittedURI.length - 2];
				try {
					page = Integer
							.parseInt(splittedURI[splittedURI.length - 1]);
				} catch (Exception e) {
					System.out.println("Error when parse page string to int.");
					page = 1;
				}
			} else {
				System.out.println("Error path.");
				response.sendRedirect("/home");
			}

			keyword = URLDecoder.decode(keyword, "UTF8");
			System.out.println("Keyword to search : " + keyword);			
			Tag tag = TagModel.getByAlias(keyword);
			if (tag != null) {
				List<Tube> result = new ArrayList<Tube>();
				int startPoint = (page - 1) * itemPerPage;
				if (tag.getListTubeId().size() > startPoint) {
					for (int i = startPoint; i < tag.getListTubeId().size(); i++) {
						if (i == (itemPerPage + startPoint)) {
							break;
						}
						String id = tag.getListTubeId().get(i).getValue();
						System.out.println(id);
						Tube tub = TubeModel.getByTubeId(id);
						if (tub != null) {
							result.add(tub);
						}
					}
				} else {
					page = 1;
					for (int i = 0; i < tag.getListTubeId().size(); i++) {
						if (i == itemPerPage) {
							break;
						}
						String id = tag.getListTubeId().get(i).getValue();
						Tube tub = TubeModel.getByTubeId(id);
						if (tub != null) {
							result.add(tub);
						}
					}
				}
				request.setAttribute("url", request.getRequestURL());
				request.setAttribute("result", result);
				request.setAttribute("currentPage", page);
				request.setAttribute("keyword", keyword.replaceAll("\\+", " "));
				request.setAttribute("totalResult", tag.getListTubeId().size());
				request.getRequestDispatcher("/keyword_show.jsp").forward(
						request, response);
			} else {
				System.out.println("Tag is not exits.");
				response.sendRedirect("/home");
			}
		} catch (Exception e) {
			System.out.println("Exception.");
			e.printStackTrace();
			response.sendRedirect("/home");
		}
	}

}
